package com.enjoy.book.action;


import com.enjoy.book.bean.User;
import com.enjoy.book.biz.UserBiz;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * /user.let?type=login 登录    / 代表的是项目的根目录  web
 * /user.let?type=exit  退出
 * /user.let?type=modifyPwd  修改密码
 * <p>
 * web
 * |- login.html
 * |- user.let?type = login
 * |- index.jsp
 */

@WebServlet("/user.let")
public class UserServlet extends HttpServlet {
    // 构建UserBiz对象
    UserBiz userBiz = new UserBiz();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        // 1 判断用户请求的累心为login
        String method = req.getParameter("type");
        switch (method) {
            case "login":
                // 2 从login.html中获取用户名和密码
                String name = req.getParameter("name");
                String pwd = req.getParameter("pwd");
                String valCode = req.getParameter("valcode");

                // 提取session中的验证码
                String code = session.getAttribute("code").toString();
                // 不区分大小写判断
                if (!code.equalsIgnoreCase(valCode)) {
                    out.println("<script>alert('验证码不正确');location.href = 'login.html';</script>");
                    return;
                }

                // 3 调用UserBiz的getUser方法  根据用户名和密码获取对应的用户对象
                User user = userBiz.getUser(name, pwd);

                // 4 判断用户对象是否为null
                if (user == null) {

                    // 4.1 如果是null表示用户名或密码不正确，提示错误，返回登录界面
                    out.println("<script>alert('用户名或密码不正确');location.href = 'login.html';</script>");

                } else {
                    // 4.2 如果是非空，表示登陆成功，将用户对象保存到session中，提示登陆成功后，将页面跳转到index.jsp
                    session.setAttribute("user", user);
                    out.println("<script>alert('登陆成功');location.href = 'index.jsp';</script>");

                }
                break;

            case "exit":
                // 清除session
                session.invalidate();

                // 跳转到login.html(框架的跳转)
                out.println("<script>parent.window.location.href = 'login.html';</script>");
                break;

            case "modifyPwd":
                // 修改密码

                // 1 获取用户输入的新密码
                String newpwd = req.getParameter("newpwd");

                // 2 获取用户编号
                long id = ((User) session.getAttribute("user")).getId();

                // 3 调用Biz层方法
                int count = userBiz.modifyPwd(id, newpwd);

                // 4 响应 类似exit
                if (count > 0) {
                    out.println("<script>alert('密码修改成功');parent.window.location.href = 'login.html';</script>");
                } else {
                    out.println("<script>alert('密码修改失败');</script>");
                }

                break;
        }


    }
}
